(function (angular) {
    'use strict';
    var dialogModule = angular.module('fs.dialog');
    dialogModule.directive('fsDraggable', draggableDirevtive);
    dialogModule.directive('fsResizeable', resizeableDirevtive);

    draggableDirevtive.$inject = ['$document', '$timeout'];
    function draggableDirevtive($document, $timeout) {
        return {
            restrict: 'A',
            link: link
        };
        function link($scope, $element, $attr) {
            var startX = 0, startY = 0, x = 0, y = 0, initialized = false, dialog;
            initDrag(1);
            function initDrag(count) {
                var dialogEl, titleEl, title;
                dialogEl = $element[0];// $document[0].querySelector("div[index='" + $attr.index + "'].modal");
                /*
                 * if (!dialogEl) { return; }
                 */
                titleEl = dialogEl.querySelector(".modal-header");
                /*
                 * if (!dialogEl || !titleEl) { if (count <= 20) { $timeout(function() { initDrag(++count); }, 100); } return; }
                 */
                dialog = angular.element(dialogEl);
                title = angular.element(titleEl);
                title.css({
                    position: 'relative',
                    cursor: 'move'
                });

                title.on('mousedown', function (event) {
                    // Prevent default dragging of selected content
                    event.preventDefault();
                    if (!initialized) {
                        x = dialogEl.offsetLeft;
                        y = dialogEl.offsetTop;
                        initialized = true;
                    }
                    startX = event.clientX - x;
                    startY = event.clientY - y;
                    $document.on('mousemove', mouseMove);
                    $document.on('mouseup', mouseUp);
                });
            }

            function mouseMove(event) {
                var bodyElement = $document[0].querySelector('body');
                var maxY = bodyElement.offsetHeight - startY - 20;
                var maxX = bodyElement.offsetWidth - startX - 20;
                y = event.clientY - startY;
                x = event.clientX - startX;
                if (x < 0) {
                    x = 0;
                }
                if (x > maxX) {
                    x = maxX;
                }

                if (y < -20) {
                    y = -20;
                }
                if (y > maxY) {
                    y = maxY;
                }
                $timeout(function () {
                    dialog.css({
                        left: x + 'px',
                        top: y + 'px'
                    });
                }, 0, false);
            }

            function mouseUp() {
                $document.off('mousemove', mouseMove);
                $document.off('mouseup', mouseUp);
            }
        }
    }

    resizeableDirevtive.$inject = ['$document', '$timeout'];
    function resizeableDirevtive($document, $timeout) {
        return {
            restrict: 'A',
            link: link
        };
        function link($scope, $element, $attr) {
            var startX = 0, startY = 0, dialogWidth, contentBodyWidth, contentBodyHeight, dialogEl, dialog, contentBodyEl, contentBody, right, bright, bleft, disableReszie = 0;
            initResize();
            function initResize() {
                var rightEL, brightEl, bleftEl, dialogWindowEl, contentEl, content;
                dialogWindowEl = $element[0];// $document[0].querySelector("div[index='" + $attr.index + "'].modal");
                if (!dialogWindowEl) {
                    return;
                }
                dialogEl = dialogWindowEl.querySelector(".modal-dialog");
                if (!dialogEl) {
                    return;
                }
                contentEl = dialogWindowEl.querySelector(".app-modal-content");
                if (!contentEl) {
                    return;
                }
                contentBodyEl = contentEl.querySelector(".modal-body");
                if (!contentBodyEl) {
                    return;
                }

                dialog = angular.element(dialogEl);
                content = angular.element(contentEl);
                contentBody = angular.element(contentBodyEl);

                content.append("<div class=\"resize-right\"></div>");
                content.append("<div class=\"resize-bottom-right\"></div>");
                content.append("<div class=\"resize-bottom-left\"></div></div>");

                rightEL = contentEl.querySelector(".resize-right");
                brightEl = contentEl.querySelector(".resize-bottom-right");
                bleftEl = contentEl.querySelector(".resize-bottom-left");

                right = angular.element(rightEL);
                bright = angular.element(brightEl);
                bleft = angular.element(bleftEl);

                right.on('mousedown', function (event) {
                    // Prevent default dragging of selected content
                    event.preventDefault();
                    dialogWidth = dialogEl.offsetWidth;
                    contentBodyWidth = contentBodyEl.offsetWidth;
                    contentBodyHeight = contentBodyEl.offsetHeight;
                    startX = event.clientX;
                    startY = event.clientY;
                    disableReszie = 1;
                    $document.on('mousemove', mouseHorizontalMove);
                    $document.on('mouseup', mouseUp);
                });

                bleft.on('mousedown', function (event) {
                    // Prevent default dragging of selected content
                    event.preventDefault();
                    dialogWidth = dialogEl.offsetWidth;
                    contentBodyWidth = contentBodyEl.offsetWidth;
                    contentBodyHeight = contentBodyEl.offsetHeight;
                    startX = event.clientX;
                    startY = event.clientY;
                    disableReszie = 2;
                    $document.on('mousemove', mouseVerticalMove);
                    $document.on('mouseup', mouseUp);
                });

                bright.on('mousedown', function (event) {
                    // Prevent default dragging of selected content
                    event.preventDefault();
                    dialogWidth = dialogEl.offsetWidth;
                    contentBodyWidth = contentBodyEl.offsetWidth;
                    contentBodyHeight = contentBodyEl.offsetHeight;
                    startX = event.clientX;
                    startY = event.clientY;
                    disableReszie = 0;
                    $document.on('mousemove', mouseMove);
                    $document.on('mouseup', mouseUp);

                });
            }

            function mouseHorizontalMove(event) {
                mouseMove(event);
            }

            function mouseVerticalMove(event) {
                mouseMove(event);
            }

            function mouseMove(event) {
                dialogWidth = disableReszie != 2 ? (dialogWidth + event.clientX - startX) : dialogWidth;
                contentBodyWidth = disableReszie != 2 ? (contentBodyWidth + event.clientX - startX) : contentBodyWidth;
                contentBodyHeight = disableReszie != 1 ? (contentBodyHeight + event.clientY - startY) : contentBodyHeight;
                startX = event.clientX;
                startY = event.clientY;
                $timeout(function () {
                    dialog.css({
                        width: dialogWidth + 'px',
                    });
                    contentBody.css({
                        width: contentBodyWidth + 'px',
                        height: contentBodyHeight + 'px'
                    });
                }, 0, false);
            }

            function mouseUp() {
                $document.off('mousemove', mouseMove);
                $document.off('mousemove', mouseVerticalMove);
                $document.off('mousemove', mouseHorizontalMove);
                $document.off('mouseup', mouseUp);
            }
        }
    }
})(angular);